﻿package com.amcharts
{
    import com.amcharts.axes.*;
    import com.amcharts.chartClasses.*;
    import com.amcharts.events.*;
    import flash.display.*;
    import flash.events.*;
    import mx.collections.*;
    import mx.effects.easing.*;

    public class AmXYChart extends AmRectangularChart
    {
        protected var _xAxes:Array;
        protected var _maxZoomFactor:Number = 20;
        protected var _chartScrollbarHorizontal:SimpleChartScrollbar;
        protected var _skipFix:Boolean;
        protected var _chartScrollbarVertical:SimpleChartScrollbar;
        protected var _yAxes:Array;
        static const VERSION:String = "1.8.3.1";

        public function AmXYChart()
        {
            _chartType = "xy";
            return;
        }// end function

        override protected function designMode() : void
        {
            dataProvider = new ArrayCollection([{x:1, y:4, value:1}, {x:5, y:3, value:4}, {x:2, y:6, value:2}, {x:8, y:7, value:6}]);
            var _loc_1:* = new AmGraph();
            _loc_1.xField = "x";
            _loc_1.yField = "y";
            _loc_1.valueField = "value";
            _loc_1.setStyle("bullet", "bubble");
            _loc_1.setStyle("lineAlpha", 0);
            this.setStyle("startEffect", Exponential.easeOut);
            this.setStyle("startDuration", 0);
            addGraph(_loc_1);
            return;
        }// end function

        protected function fitMultiplyer(param1:Number) : Number
        {
            if (param1 > _maxZoomFactor)
            {
                param1 = _maxZoomFactor;
            }
            return param1;
        }// end function

        override protected function processValueAxis(param1:ValueAxis, param2:Number) : void
        {
            super.processValueAxis(param1, param2);
            if (param1.orientation == "horizontal")
            {
                param1.minMaxField = "x";
            }
            else
            {
                param1.minMaxField = "y";
            }
            if (param1.position != "top")
            {
            }
            if (param1.position == "bottom")
            {
                param1.rotate = true;
            }
            param1.removeEventListener(ValueAxisEvent.AXIS_SELF_ZOOMED, handleAxisSelfZoom);
            param1.addEventListener(ValueAxisEvent.AXIS_SELF_ZOOMED, handleAxisSelfZoom);
            return;
        }// end function

        public function get maxZoomFactor() : Number
        {
            return _maxZoomFactor;
        }// end function

        protected function handleAxisSelfZoom(event:ValueAxisEvent) : void
        {
            var _loc_3:Number = NaN;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            var _loc_6:Number = NaN;
            var _loc_2:* = ValueAxis(event.target);
            if (_loc_2.orientation == "horizontal")
            {
                _loc_3 = fitMultiplyer(event.multiplyer);
                _horizontalPosition = (-event.position) / _widthMultiplyer * _loc_3;
                _loc_4 = -(_plotAreaWidth * _loc_3 - _plotAreaWidth);
                if (_horizontalPosition < _loc_4)
                {
                    _horizontalPosition = _loc_4;
                }
                _widthMultiplyer = _loc_3;
                zoomChart();
            }
            else
            {
                _loc_5 = fitMultiplyer(event.multiplyer);
                _verticalPosition = (-event.position) / _heightMultiplyer * _loc_5;
                _loc_6 = -(_plotAreaHeight * _loc_5 - _plotAreaHeight);
                if (_verticalPosition < _loc_6)
                {
                    _verticalPosition = _loc_6;
                }
                _heightMultiplyer = _loc_5;
                zoomChart();
            }
            updateScrollbars();
            return;
        }// end function

        protected function handleVerticalScrollbarZoom(event:ScrollbarEvent) : void
        {
            var _loc_2:* = fitMultiplyer(event.multiplyer);
            _verticalPosition = (-event.position) * _loc_2;
            var _loc_3:* = -(_plotAreaHeight * _loc_2 - _plotAreaHeight);
            if (_verticalPosition < _loc_3)
            {
                _verticalPosition = _loc_3;
            }
            _heightMultiplyer = _loc_2;
            zoomChart();
            return;
        }// end function

        public function set maxZoomFactor(param1:Number) : void
        {
            _maxZoomFactor = param1;
            return;
        }// end function

        protected function handleHorizontalScrollbarZoom(event:ScrollbarEvent) : void
        {
            var _loc_2:* = fitMultiplyer(event.multiplyer);
            _horizontalPosition = (-event.position) * _loc_2;
            var _loc_3:* = -(_plotAreaWidth * _loc_2 - _plotAreaWidth);
            if (_horizontalPosition < _loc_3)
            {
                _horizontalPosition = _loc_3;
            }
            _widthMultiplyer = _loc_2;
            zoomChart();
            return;
        }// end function

        override public function set chartScrollbar(param1:ChartScrollbar) : void
        {
            _chartScrollbar = param1;
            _chartScrollbarHorizontal = new SimpleChartScrollbar();
            _chartScrollbarVertical = new SimpleChartScrollbar();
            _chartScrollbarHorizontal.addEventListener(ScrollbarEvent.ZOOMED, handleHorizontalScrollbarZoom);
            _chartScrollbarVertical.addEventListener(ScrollbarEvent.ZOOMED, handleVerticalScrollbarZoom);
            _chartScrollbarHorizontal.styleName = _chartScrollbar;
            _chartScrollbarVertical.styleName = _chartScrollbar;
            _chartScrollbarVertical.updateOnReleaseOnly = _chartScrollbar.updateOnReleaseOnly;
            _chartScrollbarHorizontal.updateOnReleaseOnly = _chartScrollbar.updateOnReleaseOnly;
            _chartScrollbarVertical.rotate = true;
            _chartScrollbarHorizontal.rotate = false;
            _chartScrollbarVertical.width = _chartScrollbar.height;
            _chartScrollbarHorizontal.height = _chartScrollbar.height;
            _scrollbarHeight = _chartScrollbarHorizontal.height;
            addChild(_chartScrollbarHorizontal);
            addChild(_chartScrollbarVertical);
            invalidateDisplayList();
            return;
        }// end function

        override protected function handleCursorZoom(event:CursorEvent) : void
        {
            var _loc_2:* = _widthMultiplyer * _plotAreaWidth / event.selectionWidth;
            var _loc_3:* = _heightMultiplyer * _plotAreaHeight / event.selectionHeight;
            _loc_2 = fitMultiplyer(_loc_2);
            _loc_3 = fitMultiplyer(_loc_3);
            _horizontalPosition = (_horizontalPosition - event.selectionX) * _loc_2 / _widthMultiplyer;
            _verticalPosition = (_verticalPosition - event.selectionY) * _loc_3 / _heightMultiplyer;
            _widthMultiplyer = _loc_2;
            _heightMultiplyer = _loc_3;
            zoomChart();
            updateScrollbars();
            return;
        }// end function

        override protected function updateTrendLines() : void
        {
            var _loc_3:TrendLine = null;
            super.updateTrendLines();
            var _loc_1:* = _trendLines.length;
            var _loc_2:Number = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = _trendLines[_loc_2];
                if (!_loc_3.valueAxis)
                {
                    _loc_3.valueAxis = _yAxes[0];
                }
                if (!_loc_3.valueAxisX)
                {
                    _loc_3.valueAxisX = _xAxes[0];
                }
                _graphsContainer.addChild(_loc_3);
                _loc_2 = _loc_2 + 1;
            }
            return;
        }// end function

        override protected function updateData() : void
        {
            var _loc_1:ValueAxis = null;
            var _loc_5:ValueAxis = null;
            var _loc_6:AmGraph = null;
            var _loc_7:String = null;
            var _loc_8:Number = NaN;
            var _loc_9:Number = NaN;
            var _loc_10:Number = NaN;
            var _loc_11:Number = NaN;
            _xAxes = new Array();
            _yAxes = new Array();
            var _loc_2:* = _valueAxes.length;
            var _loc_3:Number = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_1 = _valueAxes[_loc_3];
                if (_loc_1.position != "top")
                {
                }
                if (_loc_1.position == "bottom")
                {
                    _loc_1.rotate = true;
                }
                if (_loc_1.orientation == "vertical")
                {
                    _yAxes.push(_loc_1);
                }
                if (_loc_1.orientation == "horizontal")
                {
                    _xAxes.push(_loc_1);
                }
                _loc_3 = _loc_3 + 1;
            }
            if (_yAxes.length == 0)
            {
                _loc_1 = new ValueAxis();
                _valueAxes.push(_loc_1);
                _yAxes.push(_loc_1);
            }
            if (_xAxes.length == 0)
            {
                _loc_1 = new ValueAxis();
                _loc_1.rotate = true;
                _valueAxes.push(_loc_1);
                _xAxes.push(_loc_1);
            }
            _loc_2 = _valueAxes.length;
            _loc_3 = 0;
            while (_loc_3 < _loc_2)
            {
                
                processValueAxis(_valueAxes[_loc_3], _loc_3);
                _loc_3 = _loc_3 + 1;
            }
            _loc_2 = _graphs.length;
            _loc_3 = 0;
            while (_loc_3 < _loc_2)
            {
                
                processGraph(_graphs[_loc_3], _loc_3);
                _loc_3 = _loc_3 + 1;
            }
            _dataParser.charts = [this];
            _chartDataProvider = _dataParser.parseXYData(_dataProvider);
            var _loc_4:* = _chartDataProvider.length - 1;
            if (_chartCursor)
            {
                _chartCursor.hideCursor();
                _chartCursor.dataProvider = _chartDataProvider;
                _chartCursor.type = "crosshair";
                _chartCursor.valueBalloonsEnabled = false;
            }
            if (_chartScrollbar)
            {
                _chartScrollbarVertical.dataProvider = _chartDataProvider;
                _chartScrollbarHorizontal.dataProvider = _chartDataProvider;
            }
            _loc_2 = _valueAxes.length;
            _loc_3 = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_5 = _valueAxes[_loc_3];
                _loc_5.dataProvider = _chartDataProvider;
                _loc_5.minTemp = undefined;
                _loc_5.maxTemp = undefined;
                _loc_5.zoom(0, _loc_4);
                _loc_3 = _loc_3 + 1;
            }
            _loc_2 = _graphs.length;
            _loc_3 = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_6 = _graphs[_loc_3];
                _loc_6.chartDataProvider = _chartDataProvider;
                _loc_6.zoom(0, _loc_4);
                _loc_7 = _loc_6.valueField;
                _loc_8 = _dataProvider.length;
                if (_loc_7)
                {
                    _loc_9 = 0;
                    _loc_10 = 0;
                    while (_loc_10 < _loc_8)
                    {
                        
                        _loc_11 = _dataProvider.getItemAt(_loc_10)[_loc_7];
                        if (_loc_11 > _loc_9)
                        {
                            _loc_9 = _loc_11;
                        }
                        _loc_10 = _loc_10 + 1;
                    }
                    _loc_6.maxValue = _loc_9;
                }
                _loc_3 = _loc_3 + 1;
            }
            _skipFix = true;
            addAxesListeners();
            zoomOut();
            return;
        }// end function

        override protected function handleZoomOut(event:MouseEvent) : void
        {
            if (_zoomOutPressed)
            {
                if (_chartScrollbar)
                {
                    _chartScrollbarHorizontal.stopTween();
                    _chartScrollbarVertical.stopTween();
                }
                callLater(zoomOut);
                _zoomOutPressed = false;
            }
            return;
        }// end function

        protected function toggleZoomOutButton() : void
        {
            if (_heightMultiplyer == 1)
            {
            }
            if (_widthMultiplyer == 1)
            {
                _zoomOutButton.visible = false;
            }
            else
            {
                _zoomOutButton.visible = true;
            }
            return;
        }// end function

        protected function zoomChart() : void
        {
            toggleZoomOutButton();
            updateGraphs();
            if (!_skipFix)
            {
                fixMinMax();
            }
            else
            {
                _skipFix = false;
            }
            updateValueAxes();
            updateTrendLines();
            return;
        }// end function

        override protected function createChildren() : void
        {
            super.createChildren();
            return;
        }// end function

        override protected function processGraph(param1:AmGraph, param2:Number) : void
        {
            super.processGraph(param1, param2);
            if (!param1.xAxis)
            {
                param1.xAxis = _xAxes[0];
            }
            if (!param1.yAxis)
            {
                param1.yAxis = _yAxes[0];
            }
            return;
        }// end function

        override public function removeChartScrollbar() : void
        {
            if (_chartScrollbar)
            {
                _chartScrollbar.destroy();
                _chartScrollbar = null;
                _chartScrollbarHorizontal.destroy();
                _chartScrollbarVertical.destroy();
                if (_chartScrollbarHorizontal.parent == this)
                {
                    removeChild(_chartScrollbarHorizontal);
                }
                if (_chartScrollbarVertical.parent == this)
                {
                    removeChild(_chartScrollbarVertical);
                }
            }
            return;
        }// end function

        public function updateScrollbar() : void
        {
            _updateScrollbar = true;
            _chartScrollbarHorizontal.stopTween();
            _chartScrollbarVertical.stopTween();
            return;
        }// end function

        protected function fixMinMax() : void
        {
            var _loc_3:ValueAxis = null;
            var _loc_1:* = _valueAxes.length;
            var _loc_2:Number = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = _valueAxes[_loc_2];
                if (_loc_3.logarithmic == true)
                {
                    if (!isNaN(_loc_3.minReal))
                    {
                        _loc_3.minTemp = _loc_3.minReal;
                    }
                }
                else if (!isNaN(_loc_3.min))
                {
                    _loc_3.minTemp = _loc_3.min;
                }
                if (!isNaN(_loc_3.max))
                {
                    _loc_3.maxTemp = _loc_3.max;
                }
                _loc_2 = _loc_2 + 1;
            }
            _skipFix = false;
            return;
        }// end function

        override protected function updateValueAxes() : void
        {
            var _loc_3:ValueAxis = null;
            var _loc_4:Number = NaN;
            var _loc_5:Number = NaN;
            var _loc_6:Number = NaN;
            super.updateValueAxes();
            var _loc_1:* = _valueAxes.length;
            var _loc_2:Number = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = _valueAxes[_loc_2];
                _loc_3.validateNow();
                if (!isNaN(_loc_3.min))
                {
                }
                if (!isNaN(_loc_3.max))
                {
                    if (_loc_3.rotate == false)
                    {
                        _loc_4 = _loc_3.coordinateToValue(-_verticalPosition);
                        _loc_5 = _loc_3.coordinateToValue(-_verticalPosition + _plotAreaHeight);
                    }
                    else
                    {
                        _loc_4 = _loc_3.coordinateToValue(-_horizontalPosition);
                        _loc_5 = _loc_3.coordinateToValue(-_horizontalPosition + _plotAreaWidth);
                    }
                    if (!isNaN(_loc_4))
                    {
                    }
                    if (!isNaN(_loc_5))
                    {
                        if (_loc_4 > _loc_5)
                        {
                            _loc_6 = _loc_5;
                            _loc_5 = _loc_4;
                            _loc_4 = _loc_6;
                        }
                        _loc_3.dispatchZoomEvent(_loc_4, _loc_5);
                    }
                }
                _loc_2 = _loc_2 + 1;
            }
            return;
        }// end function

        override protected function updateScrollbars() : void
        {
            if (_chartScrollbar)
            {
                if (_xAxes[0].position != "bottom")
                {
                }
                if (_xAxes[0].position == "left")
                {
                    _chartScrollbarHorizontal.y = _marginTop - _scrollbarHeight + _dy;
                    _chartScrollbarHorizontal.x = _marginLeft + _dx;
                }
                else
                {
                    _chartScrollbarHorizontal.y = _marginTop + _plotAreaHeight;
                    _chartScrollbarHorizontal.x = _marginLeft;
                }
                if (_yAxes[0].position == "left")
                {
                    _chartScrollbarVertical.y = _marginTop + _dy;
                    _chartScrollbarVertical.x = _marginLeft + _plotAreaWidth + _dx;
                }
                else
                {
                    _chartScrollbarVertical.y = _marginTop;
                    _chartScrollbarVertical.x = _marginLeft - _scrollbarHeight;
                }
                _chartScrollbarVertical.height = _plotAreaHeight;
                _chartScrollbarHorizontal.width = _plotAreaWidth;
                _chartScrollbarHorizontal.relativeZoom(_widthMultiplyer, (-_horizontalPosition) / _widthMultiplyer);
                _chartScrollbarVertical.relativeZoom(_heightMultiplyer, (-_verticalPosition) / _heightMultiplyer);
            }
            return;
        }// end function

        override protected function updateDepths() : void
        {
            super.updateDepths();
            this.setChildIndex(_plotArea, 0);
            this.setChildIndex(_valueAxesContainer, (this.numChildren - 1));
            this.setChildIndex(_graphsContainer, (this.numChildren - 1));
            this.setChildIndex(_grid0, (this.numChildren - 1));
            this.setChildIndex(_valuesContainer, (this.numChildren - 1));
            this.setChildIndex(_labelsContainer, (this.numChildren - 1));
            this.setChildIndex(DisplayObject(_zoomOutButton), (this.numChildren - 1));
            this.setChildIndex(_balloon, (this.numChildren - 1));
            return;
        }// end function

        public function zoomOut() : void
        {
            _horizontalPosition = 0;
            _verticalPosition = 0;
            _widthMultiplyer = 1;
            _heightMultiplyer = 1;
            zoomChart();
            updateScrollbars();
            return;
        }// end function

        override protected function updateMargins() : void
        {
            super.updateMargins();
            if (_chartScrollbar)
            {
                if (_xAxes[0].position != "bottom")
                {
                }
                if (_xAxes[0].position == "left")
                {
                    _marginTop = _marginTop + _scrollbarHeight;
                }
                else
                {
                    _marginBottom = _marginBottom + _scrollbarHeight;
                }
                if (_yAxes[0].position == "left")
                {
                    _marginRight = _marginRight + _scrollbarHeight;
                }
                else
                {
                    _marginLeft = _marginLeft + _scrollbarHeight;
                }
            }
            return;
        }// end function

    }
}
